package com.movitech.mobile.cz.base.supplier.bizcontroller;

import com.movitech.mobile.cz.base.supplier.entity.SSupplierRcRelate;
import com.movitech.mobile.cz.base.supplier.entity.SSuppliers;
import com.movitech.mobile.cz.base.supplier.entity.ext.SuppliersListRequestVO;
import com.movitech.mobile.cz.base.supplier.entity.ext.SuppliersUpdatePasswordRequestVO;
import com.movitech.mobile.cz.base.supplier.entity.ext.SuppliersUpdateStaticRequestVO;
import com.movitech.mobile.cz.base.supplier.service.SSupplierRcRelateService;
import com.movitech.mobile.cz.base.common.aop.ServiceThrowable;
import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.supplier.bizservice.SSuppliersIntranetService;
import com.movitech.mobile.cz.base.supplier.bizservice.SSuppliersWorkFlowService;
import com.movitech.mobile.cz.base.supplier.bizvo.*;
import com.movitech.mobile.cz.base.sys.entity.ext.ExtSysUser;
import com.movitech.mobile.cz.base.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * SSuppliers 供方内网管理 前端控制器
 *
 * @author Blue.Xia
 * @since 2018-03-05
 */
@RestController
@RequestMapping("/v1/sSuppliersIntranet")
@Api(value = "供方内网管理接口", description = "供方内网管理接口")
public class SSuppliersIntranetController {

    @Autowired
    private SSuppliersIntranetService sSuppliersIntranetService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    @Qualifier("sSuppliersWorkFlowService")
    private SSuppliersWorkFlowService sSuppliersWorkFlowService;
    
    @Autowired
    private SSupplierRcRelateService sSupplierRcRelateService;

    @PostMapping("/addSuppliersIntranet")
    @ApiOperation(
            value = "供方信息新增接口",
            notes = "供方信息新增接口",
            response = SSuppliers.class
    )
    public RestReturnResult addSuppliersIntranet(@RequestBody SuppliersIntranetRequestVO suppliersIntranetRequestVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        try{
            ExtSysUser sysUser = sysUserService.getUserByLoginName(suppliersIntranetRequestVO.getLoginName());
            if (sysUser != null) {
                restReturnResult = RestReturnResult.error();
                restReturnResult.setMsg("登录名已存在！");
                return restReturnResult;
            }
            boolean updateFlag = sSuppliersIntranetService.insertSupplierIntranet(suppliersIntranetRequestVO);

            if (!updateFlag) {
                restReturnResult = RestReturnResult.error();
            }else{
                restReturnResult=RestReturnResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            restReturnResult = RestReturnResult.error();
        }
        return restReturnResult;
    }

    @PostMapping("/listSuppliersIntranet")
    @ApiOperation(
            value = "供方信息列表接口",
            notes = "供方信息列表接口",
            response = SSuppliers.class
    )
    public RestReturnResult listSuppliersIntranet(@RequestBody SuppliersListRequestVO suppliersListRequestVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        PageCode pageCode = sSuppliersIntranetService.findSuppliersList(suppliersListRequestVO);
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("list", pageCode);
        restReturnResult.setData(pageCode);
        return restReturnResult;
    }

    @PostMapping("/updateSuppliersStatic")
    @ApiOperation(value = "修改供方状态接口", notes = "修改供方状态接口")
    public RestReturnResult updateSuppliersStatic(@RequestBody SuppliersUpdateStaticRequestVO suppliersUpdateStaticRequestVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();

        boolean updateFlag = sSuppliersIntranetService.updateSuppliersStatic(suppliersUpdateStaticRequestVO);

        if (!updateFlag) {
            restReturnResult = RestReturnResult.error();
        }
        return restReturnResult;
    }

    @PostMapping("/updateSuppliersPassword")
    @ApiOperation(value = "修改供方密码接口", notes = "修改供方密码接口")
    public RestReturnResult updateSuppliersPassword(@RequestBody SuppliersUpdatePasswordRequestVO suppliersUpdatePasswordRequestVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();

        boolean updateFlag = sSuppliersIntranetService.updateSuppliersPassword(suppliersUpdatePasswordRequestVO);

        if (!updateFlag) {
            restReturnResult = RestReturnResult.error();
        }
        return restReturnResult;
    }

    @PostMapping("/qualificationInspection")
    @ApiOperation(value = "供方资质审查处理接口", notes = "供方资质审查处理接口")
    public RestReturnResult qualificationInspection(@RequestBody SuppliersAuditRequestVO suppliersAuditRequestVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();

        SSupplierRcRelate sSupplierRcRelate=sSupplierRcRelateService.selectById(suppliersAuditRequestVO.getRcRelatedId());
        if(sSupplierRcRelate != null){
        	suppliersAuditRequestVO.setId(sSupplierRcRelate.getSuppliersId());
        	restReturnResult = sSuppliersWorkFlowService.qualificationInspection(suppliersAuditRequestVO);
        }else{
        	throw new RuntimeException("供方数据不存在", new ServiceThrowable());
        }
        
        return restReturnResult;
    }

}
